package com.jh.paymentchannel.helipay.service;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jh.paymentchannel.helipay.vo.TaskVo;
import com.jh.paymentchannel.util.Util;
import com.jh.paymentchannel.util.jifu.DateTime;

import cn.jh.common.utils.CommonConstants;

@Controller
public class Time_Service {
	@Autowired
	Util util;
	private static final Logger log = LoggerFactory.getLogger(Time_Service.class);
	@RequestMapping(method=RequestMethod.POST,value="/v1.0/paymentchannel/time/more")
	public @ResponseBody Object theWayOnTime(
			@RequestParam(value="time") String[] time,
			@RequestParam(value="type") String type,
			@RequestParam(value="amount") String amount,
			@RequestParam(value="money") String money,
			@RequestParam(value="extraFee") String extraFee,
			@RequestParam(value="withdrawFee") String withdrawFee,
			@RequestParam(value="avaliable") String avaliable,
			@RequestParam(value="rate") String rate,
			@RequestParam(value="times") int times
			) throws ParseException {
		if(type.equals("2")) {
			Map map = new HashMap();
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
		    map.put(CommonConstants.RESP_MESSAGE, "此功能已下线");
			return map;
		}
			
		DecimalFormat decimalFormat = new DecimalFormat("0.00");
		decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
		//amount = String.valueOf(decimalFormat.format(Float.valueOf(amount)-Float.valueOf(extraFee)-Float.valueOf(withdrawFee)));
		Map<String, Object> map = new HashMap<String, Object>();
		List<TaskVo> list = new ArrayList<TaskVo>();
		int num = amount.indexOf(".");
		Double littlebalance = Double.parseDouble("0."+amount.substring(num+1, amount.length()));//小数金额
		String amountZ = amount.substring(0, num);//去掉小数金额
		int surplus = Integer.valueOf(amountZ)%times;//整除余数
		Double lastSurplus = surplus+littlebalance;
		int amountEll = Integer.valueOf(amountZ)-surplus;
		int amountE = (int)(Double.valueOf(amountEll)/Double.valueOf(times));
		log.info("===================="+"surplus:"+surplus+"lastSurplus:"+lastSurplus+"amountEll:"+amountEll+"amountE:"+amountE);
		int surplus2 = Integer.valueOf(times)%time.length;//整除余数
		int MMALL = 0;
		Float timemoney = 0f;
		int clc = Integer.valueOf(avaliable)-amountE-1;
		for(int j=0;j<time.length;j++) {
			int fenpeiTime = (int)(Double.valueOf(times-surplus2)/Double.valueOf(time.length));
			log.info("===================="+"surplus2:"+surplus2+"fenpeiTime:"+fenpeiTime);
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
			Date today = new Date();
			long sstime = 0;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			SimpleDateFormat sdff = new SimpleDateFormat("yyyy-MM-dd");
			Calendar c = Calendar.getInstance();
			Date dday = sdff.parse(time[j]);
			time[j] = sdff.format(dday);
			String checkDay = time[j];
			if(checkDay.trim().length()<10)
				checkDay = checkDay.substring(0, 8)+"0"+checkDay.substring(8,9);
			if(checkDay.trim().equals(sdf2.format(today))) {
				if(Integer.valueOf(DateTime.getNow("HH"))<9) {
					time[j] = time[j].trim()+" 09:00";
				}else {
					time[j] = time[j].trim()+DateTime.getNow(" HH:mm");
				}
			}else {
				time[j] = time[j].trim()+" "+String.valueOf((int)(9+Math.random()*3))+":00";
			}
			Date date = sdf.parse(time[j]);
			sstime = date.getTime();
			Date dat = null;
			if(j==time.length-1)
				fenpeiTime = fenpeiTime+surplus2;
			if(surplus2>0) {
				fenpeiTime+=1;
				surplus2--;
			}
			int jkl2 = 0;
			for(int i=0;i<fenpeiTime;i++) {
				int SJS = 0;
				int jkl = 0;
					if(fenpeiTime%2!=0) {
						jkl = (int)(1+Math.random()*clc);
						/*if(i==0&&j==0)
							jkl = Integer.valueOf(avaliable)-amountE-1;*/
						clc = clc-jkl;
					}else {
						jkl = (int)(1+Math.random()*clc);
						/*if(i==0&&j==0)
							jkl = Integer.valueOf(avaliable)-amountE-1;*/
					}
					 if(i==0) {
						 if(jkl>=amountE) 
							 jkl = amountE-1;
						 jkl2 = jkl;
					 }
					 if(i==1) {
						 jkl=-jkl2;
					 }
					 SJS = (int)(Integer.valueOf(amountE)+jkl);
					 if(SJS>Integer.valueOf(avaliable)) {
						SJS = Integer.valueOf(avaliable)-1;
					 }
					int MM = Integer.valueOf(amountE)-SJS;
					MMALL += MM;
				
				TaskVo taskvo = new TaskVo();
				
				if(i==fenpeiTime-1&&j==time.length-1) {
					taskvo.setAmount(decimalFormat.format(SJS+littlebalance+MMALL));
					if(type.equals("1")) {
						if(timemoney==0.0f) {
							taskvo.setRealamount(String.valueOf(money));
						}else {
							int end = String.valueOf(timemoney).indexOf(".");
							timemoney = Float.valueOf((String.valueOf(timemoney)+"0").substring(0, end+3));
							String rmount = decimalFormat.format(Float.valueOf(money)-timemoney);
							taskvo.setRealamount(rmount);
						}
						//taskvo.setRealamount(decimalFormat.format((SJS+lastSurplus+MMALL-((SJS+lastSurplus+MMALL)*Float.valueOf(rate)+Float.valueOf(extraFee)*2+Float.valueOf(withdrawFee)*2))-Float.valueOf(decimalFormat.format((SJS+lastSurplus+MMALL-((SJS+lastSurplus+MMALL)*Float.valueOf(rate)+Float.valueOf(extraFee)*2+Float.valueOf(withdrawFee)*2))*0.0002+0.6))));
					}
					if(type.equals("2")) {
						if(timemoney==0.0f) {
							taskvo.setRealamount(String.valueOf(money));
						}else {
							int end = String.valueOf(timemoney).indexOf(".");
							timemoney = Float.valueOf((String.valueOf(timemoney)+"0").substring(0, end+3));
							String rmount = decimalFormat.format(Float.valueOf(money)-timemoney);
							taskvo.setRealamount(rmount);
						}
						//taskvo.setRealamount(decimalFormat.format((SJS+lastSurplus+MMALL-((SJS+lastSurplus+MMALL)*Float.valueOf(rate)+Float.valueOf(extraFee)+Float.valueOf(withdrawFee)))-Float.valueOf(decimalFormat.format((SJS+lastSurplus+MMALL-((SJS+lastSurplus+MMALL)*Float.valueOf(rate)+Float.valueOf(extraFee)+Float.valueOf(withdrawFee)))*0.0002+0.6))));
					}
				}else {
					if(surplus!=0) {
						SJS += 1;
						surplus--;
					}
					taskvo.setAmount(decimalFormat.format(SJS));
					//realamount为扣除万2+0.6后的实际还款金额
					/*if(type.equals("1")) {
						String www = decimalFormat.format((SJS-(SJS*Float.valueOf(rate)+Float.valueOf(extraFee)*2+Float.valueOf(withdrawFee)*2)));
						taskvo.setRealamount(www);
						timemoney += Float.valueOf(www);
					}
					if(type.equals("2")) {*/
						String www = decimalFormat.format((SJS-(SJS*Float.valueOf(rate)+Float.valueOf(extraFee)+Float.valueOf(withdrawFee))));
						taskvo.setRealamount(www);
						timemoney += Float.valueOf(www);
					//}
				}
				if(!checkDay.equals(sdf2.format(today))) {
					if(i==0)
						sstime += (3600000+Math.random()*3600000);
					if(i==1)
						sstime += (3600000*(2+Math.random()*3)+Math.random()*3600000);
				}else {
					if(Integer.valueOf(DateTime.getNow("HH"))<10) {
						if(i==0)
							sstime += (3600000+Math.random()*3600000);
						if(i==1)
							sstime += (7200000+Math.random()*1800000);
					}else {
						if(i==0)
							sstime += 7200000;
						if(i==1)
							sstime += (7200000+Math.random()*1800000);
					}
				}
				c.setTimeInMillis(sstime); 
				dat = c.getTime();
				time[j] = sdf.format(dat);
				taskvo.setTime(time[j]);
				if(type.equals("1"))
					taskvo.setType("1");//还款
				if(type.equals("2"))
					taskvo.setType("2");//消费
				list.add(taskvo);
			}
		}
		String front = null;
		String back = null;
		String front2 = null;
		String back2 = null;
		for(int k=0;k<list.size()-1;k++) {
			for(int n=0;n<list.size()-k-1;n++) {
				front = list.get(n).getAmount();
				back = list.get(n+1).getAmount();
				front2 = list.get(n).getRealamount();
				back2 = list.get(n+1).getRealamount();
				if(Double.valueOf(front)<Double.valueOf(back)) {
					list.get(n).setAmount(back);
					list.get(n+1).setAmount(front);
					list.get(n).setRealamount(back2);
					list.get(n+1).setRealamount(front2);
				}
			}
		}
		map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
		map.put(CommonConstants.RESULT, list);
		return map;
	}
}
